package com.casic.modules.activiti.service;

import com.casic.modules.sys.entity.SysDeptEntity;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysDeptService;
import com.casic.modules.sys.service.SysUserService;
import com.casic.modules.sys.shiro.ShiroUtils;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Activiti User Entity Service
 * @author ThinkGem
 * @version 2013-11-03
 */
@Service("actUserEntityService")
public class ActUserEntityService extends UserEntityManager {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysDeptService sysDeptService;
	
	@Override
	public Boolean checkPassword(String account, String password) {
		// TODO Auto-generated method stub
        SysUserEntity userEntity=new SysUserEntity();
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        userEntity.setSalt(salt);
        userEntity.setPassword(ShiroUtils.sha256(password, userEntity.getSalt()));
		SysUserEntity user= sysUserService.queryByUserName(account);
		if(user==null){
		    return false;
        }else if(!userEntity.getPassword().equals(user.getPassword())){
            return false;
        }else {
            return true;
        }
	}
	@Override  
    public UserEntity findUserById(final String userCode) {  
        if (userCode == null)  
            return null;  
        try {  
            UserEntity userEntity = new UserEntity();
            SysUserEntity sysUser=sysUserService.queryObject(userCode);
            userEntity.setId(sysUser.getUserId()+"");
            userEntity.setPassword(sysUser.getPassword());
            userEntity.setLastName(sysUser.getUsername());
            return userEntity;  
        } catch (EmptyResultDataAccessException e) {
            return null;  
        }  
    }  
  
    @Override  
    public List<Group> findGroupsByUser(final String userCode) {  
        if (userCode == null){
        	 return null;
        }
        SysUserEntity sysUser=sysUserService.queryObject(userCode);
        SysDeptEntity sysDept=sysDeptService.queryObject(sysUser.getDeptId()+"");
        List<Group> listGroup=new ArrayList<Group>();
        GroupEntity groupEntity = new GroupEntity();
        groupEntity.setId(sysDept.getDeptId()+"");
        groupEntity.setName(sysDept.getName());
        listGroup.add(groupEntity);
        return listGroup;
    }  
  
    @Override  
    public List<User> findUserByQueryCriteria(UserQueryImpl query, Page page) {  
        throw new RuntimeException("not implement method.");  
    }  
  
    @Override  
    public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId,  
            String key) {  
        throw new RuntimeException("not implement method.");  
    }  
  
    @Override  
    public List<String> findUserInfoKeysByUserIdAndType(String userId,  
            String type) {  
        throw new RuntimeException("not implement method.");  
    }  
  
    @Override  
    public long findUserCountByQueryCriteria(UserQueryImpl query) {  
        throw new RuntimeException("not implement method.");  
    } 
}
